package com.kdd.campus.sh.controller;

import com.kdd.campus.common.entity.CommonResult;
import com.kdd.campus.sh.po.ShCategory;
import com.kdd.campus.sh.service.ShCategoryService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "分类")
@RestController
@RequestMapping("/category")
@CrossOrigin
public class ShCategoryController {

    @Autowired
    private ShCategoryService shCategoryService;

    /***
     * 查询所有分类
     * @return
     */
    @ApiOperation(value = "查询分类",notes = "根据location查询首页分类或全部分类",tags = {"ShCategoryController"})
    @ApiImplicitParam(paramType = "query" , name = "location" , value = "分类的位置(0:首页),如果不传就是查询全部分类" , dataType = "Integer")
    @GetMapping
    public CommonResult<List<ShCategory>> findAll(Integer location){
        //调用ShCategoryService实现查询所有ShCategory
        System.out.println(location);
        List<ShCategory> list = shCategoryService.findByLocation(location);
        return CommonResult.success(list);
    }

    /***
     * 查询子分类分类
     * @return
     */
    @ApiOperation(value = "查询子分类")
    @ApiImplicitParam(paramType = "path" , name = "parentId" , value = "父分类的id" , dataType = "Integer")
    @GetMapping("/{parentId}")
    public CommonResult<List<ShCategory>> findByParentId(@PathVariable Integer parentId){
        //调用ShCategoryService实现条件查询
        List<ShCategory> list = shCategoryService.findList(ShCategory.builder().parentId(parentId).build());
        return CommonResult.success(list);
    }

    @ApiOperation(value = "获取某个一级分类下商品数量")
    @ApiImplicitParam(paramType = "query" , name = "categoryId" , value = "一级分类的id" , dataType = "Integer")
    @GetMapping("/productCount/{categoryId}")
    public CommonResult getProductCountByCategory(Integer categoryId){
        int result = shCategoryService.getProductCountByCategory(categoryId);
        return CommonResult.success(result);
    }
}
